<template>
    <div>
        <el-table border :data="permidata" row-key="id">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="权限值" prop="code"></el-table-column>
            <el-table-column label="修改时间" prop="updateTime"></el-table-column>
            <el-table-column label="操作">
                <template #="{ row, $index }">
                    <el-button type="primary" @click="addpermission(row)" size="small" icon="UserFilled"
                        :disabled="row.level == 4 ? true : false">
                        增加菜单
                    </el-button>
                    <el-button type="primary" @click="updatedialog(row)" size="small" icon="Check"
                        :disabled="row.level == 1 ? true : false">
                        编辑
                    </el-button>
                    <el-popconfirm :title="`确定要删除 ${row.name} 吗?`" icon="DeleteFilled" icon-color="#f56c6c"
                        @confirm="deletepermiss(row.id)">
                        <template #reference>
                            <el-button type="danger" size="small" icon="Delete">删除</el-button>
                        </template>
                    </el-popconfirm>
                    <!-- <el-button type="primary" size="small" icon="Check"  :disabled="row.level==1?true:false" >
                            删除
                        </el-button> -->
                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="dialogVisible" tile="增加菜单">
            <el-form>
                <el-form-item label="名称">
                    <el-input placeholder="请你输入菜单名称" v-model="menuParams.name"></el-input>
                </el-form-item>
                <el-form-item label="名称">
                    <el-input placeholder="请你输入菜单名称" v-model="menuParams.code"></el-input>
                </el-form-item>

            </el-form>
            <template #footer>
                <el-button type="primary" size="default" @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" size="default" @click="save">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import { getpermission, addsave, update, remove } from '@/api/acl/menu.js'
import { onMounted, ref, reactive } from 'vue';
let permidata = ref([])
let dialogVisible = ref(false)
// 添加或修改菜单的携带参数
let menuParams = reactive({
    id: 0,
    code: '',
    pid: 0,
    name: '',
    level: 0
})

onMounted(() => {
    getHaspermission()
})

const getHaspermission = () => {
    getpermission().then(res => {
        console.log(res);
        if (res.code == 200) {
            permidata.value = res.data
        }
    })
}
const addpermission = (row) => {
    console.log(row);
    dialogVisible.value = true
    Object.assign(menuParams, {
        id: 0,
        code: '',
        pid: 0,
        name: '',
        level: 0
    })
    menuParams.level = row.level + 1;
    menuParams.pid = row.id
    console.log(menuParams);
}
const updatedialog = (row) => {
    console.log(row);
    dialogVisible.value = true
    Object.assign(menuParams, row)

}
const save = async () => {
    console.log(menuParams);
    if (menuParams.id) {
        let result = await update(menuParams)
        console.log(result);
        if (result.code == 200) {
            dialogVisible.value = false
            getHaspermission()
            ElNotification({
                message: '成功',
                type: 'success',
                title: '修改成功'
            })
        }

    } else {
        let result = await addsave(menuParams)
        console.log(result);
        if (result.code == 200) {
            dialogVisible.value = false
            getHaspermission()
            ElNotification({
                message: '成功',
                type: 'success',
                title: '增加成功'
            })
        }
    }

}
const deletepermiss = async (id) => {
    let result = await remove(id)
    console.log(result);
    if (result.code == 200) {
        dialogVisible.value = false
        getHaspermission()
        ElNotification({
            message: '成功',
            type: 'success',
            title: '删除成功'
        })
    }
}
</script>